<template>
  <div class="page-container">
    <div class="page-header">
      <h2>商品管理</h2>
      <div class="decoration"></div>
    </div>
    
    <div class="table-container">
      <div class="table-header">
        <div class="search-box">
          <input 
            type="text" 
            v-model="searchForm.name"
            placeholder="搜索商品名称..."
            @input="handleSearch"
          >
          <select v-model="searchForm.source" @change="handleSearch">
            <option value="">全部来源</option>
            <option value="自营">自营</option>
            <option value="第三方">第三方</option>
          </select>
        </div>
        <button class="add-btn">
          <span class="btn-icon">+</span>
          添加商品
        </button>
      </div>
      
      <div class="table-wrapper">
        <table class="data-table">
          <thead>
            <tr>
              <th>商品ID</th>
              <th>商品名称</th>
              <th>商品价格</th>
              <th>库存量</th>
              <th>商品来源</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr 
              v-for="product in productList" 
              :key="product.id"
              class="table-row"
            >
              <td>{{ product.id }}</td>
              <td>{{ product.name }}</td>
              <td class="price">¥{{ product.price.toFixed(2) }}</td>
              <td>
                <span 
                  class="stock-badge"
                  :class="getStockClass(product.stock)"
                >
                  {{ product.stock }}
                </span>
              </td>
              <td>{{ product.source }}</td>
              <td class="actions">
                <button class="action-btn edit">编辑</button>
                <button class="action-btn delete">删除</button>
              </td>
            </tr>
            <tr v-if="productList.length === 0">
              <td colspan="6" class="empty-text">暂无数据</td>
            </tr>
          </tbody>
        </table>
      </div>
      
      <!-- 分页组件 -->
      <div class="pagination">
        <el-pagination
          v-model:current-page="pagination.current"
          v-model:page-size="pagination.size"
          :total="pagination.total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getProducts } from '@/api/product'

// 搜索表单
const searchForm = reactive({
  name: '',
  source: '',
  page: 1,
  size: 10
})

// 商品列表数据
const productList = ref([])

// 分页信息
const pagination = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 加载状态
const loading = ref(false)

// 获取商品列表
const fetchProducts = async () => {
  try {
    loading.value = true
    const { records, total, size, current } = await getProducts({
      ...searchForm,
      page: pagination.current,
      size: pagination.size
    })
    productList.value = records
    pagination.total = total
    pagination.size = size
    pagination.current = current
  } catch (error) {
    ElMessage.error('获取商品列表失败')
  } finally {
    loading.value = false
  }
}

// 处理搜索
const handleSearch = () => {
  pagination.current = 1
  fetchProducts()
}

// 处理页码变化
const handleCurrentChange = (page) => {
  pagination.current = page
  fetchProducts()
}

// 处理每页条数变化
const handleSizeChange = (size) => {
  pagination.size = size
  pagination.current = 1
  fetchProducts()
}

// 获取库存状态样式
const getStockClass = (stock) => {
  if (stock === 0) return 'out-of-stock'
  if (stock < 50) return 'low-stock'
  return 'in-stock'
}

// 页面加载时获取数据
onMounted(() => {
  fetchProducts()
})
</script>

<style scoped>
.page-container {
  background: white;
  padding: 24px;
  border-radius: 16px;
  min-height: 500px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.05);
  animation: slideUp 0.5s ease-out;
}

.page-header {
  position: relative;
  margin-bottom: 30px;
  padding-bottom: 16px;
}

.page-header h2 {
  font-size: 24px;
  font-weight: 600;
  color: #333;
  margin: 0;
}

.decoration {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 60px;
  height: 3px;
  background: linear-gradient(90deg, #4caf50, #a5d6a7);
  border-radius: 3px;
}

.table-container {
  margin-top: 20px;
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.search-box {
  display: flex;
  gap: 12px;
}

.search-box input {
  padding: 10px 16px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  width: 240px;
  font-size: 14px;
  transition: all 0.3s;
}

.search-box input:focus {
  outline: none;
  border-color: #4caf50;
  box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.1);
}

.search-box select {
  padding: 10px 16px;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  font-size: 14px;
  transition: all 0.3s;
  background-color: white;
  cursor: pointer;
}

.search-box select:focus {
  outline: none;
  border-color: #4caf50;
  box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.1);
}

.add-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background-color: #4caf50;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: all 0.3s;
}

.add-btn:hover {
  background-color: #45a049;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(76, 175, 80, 0.2);
}

.btn-icon {
  font-size: 18px;
  font-weight: bold;
}

.table-wrapper {
  overflow-x: auto;
  border-radius: 8px;
  border: 1px solid #e0e0e0;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

.data-table th {
  background-color: #f5f5f5;
  padding: 12px 16px;
  font-weight: 600;
  color: #333;
  font-size: 14px;
}

.data-table td {
  padding: 16px;
  border-top: 1px solid #e0e0e0;
  color: #666;
  font-size: 14px;
}

.table-row {
  transition: background-color 0.3s;
}

.table-row:hover {
  background-color: #f8f8f8;
}

.price {
  color: #f57c00;
  font-weight: 500;
}

.stock-badge {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
}

.in-stock {
  background-color: #e8f5e9;
  color: #2e7d32;
}

.low-stock {
  background-color: #fff3e0;
  color: #ef6c00;
}

.out-of-stock {
  background-color: #ffebee;
  color: #c62828;
}

.actions {
  display: flex;
  gap: 8px;
}

.action-btn {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 500;
  transition: all 0.3s;
}

.action-btn.edit {
  background-color: #e3f2fd;
  color: #1976d2;
}

.action-btn.edit:hover {
  background-color: #bbdefb;
}

.action-btn.delete {
  background-color: #ffebee;
  color: #c62828;
}

.action-btn.delete:hover {
  background-color: #ffcdd2;
}

.empty-text {
  text-align: center;
  color: #999;
  padding: 32px 0;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style> 